﻿using CaseManagerLibrary.Http;
using CaseManagerLibrary.Models;
using NetSDKCS;
using PSDLib.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSDManager.SystemSettings
{
    public partial class FrmChannel : Form
    {

        public List<string> Rooms { get; set; }

        //public List<int> ChannelIds { get; set; }

        public dl_room_config ChannelSetting { get; set; }

        public FrmChannel()
        {
            InitializeComponent();
        }

        public void SetRooms(List<string> rooms)
        {
            foreach (string room in rooms)
            {
                cmbRoom.Items.Add(room);
            }

            if (cmbRoom.Items.Count > 0)
            {
                cmbRoom.SelectedIndex = 0;
            }
        }

        public void SetChannels(List<PSDLib.Download.ChannelInfo> channels)
        {
            if (channels != null)
            {
                foreach (PSDLib.Download.ChannelInfo ch in channels)
                {
                    cmbChannel.Items.Add(ch);
                }

                cmbChannel.DisplayMember = "Name";
                cmbChannel.ValueMember = "Id";
            }

            if (cmbChannel.Items.Count > 0)
            {
                cmbChannel.SelectedIndex = 0;
            }
        }

        public void SetChannelSetting(string id, string room, string channelName, string channelId)
        {
            cmbRoom.SelectedIndex = -1;
            txtChannel.Text = "";
            cmbChannel.SelectedIndex = -1;

            ChannelSetting = new dl_room_config();
            ChannelSetting.id = id;

            for (int i = 0; i < cmbRoom.Items.Count; i++)
            {
                if (cmbRoom.Items[i].ToString() == room)
                {
                    cmbRoom.SelectedIndex = i;
                    break;
                }
            }

            txtChannel.Text = channelName;

            for (int i = 0; i < cmbChannel.Items.Count; i++)
            {
                if ((cmbChannel.Items[i] as PSDLib.Download.ChannelInfo).Id == channelId)
                {
                    cmbChannel.SelectedIndex = i;
                    break;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ChannelSetting = null;

            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            if (cmbRoom.SelectedIndex < 0)
            {
                MessageBox.Show("请选择房间号");
                return;
            }

            if (txtChannel.Text.Trim() == "")
            {
                MessageBox.Show("请填写通道名称");
                return;
            }

            if (cmbChannel.SelectedIndex < 0)
            {
                MessageBox.Show("请选择通道号");
                return;
            }

            if (ChannelSetting == null)
            {
                ChannelSetting = new dl_room_config();
                ChannelSetting.id = Guid.NewGuid().ToString();
            }

            ChannelSetting.room_num = cmbRoom.SelectedItem.ToString();
            ChannelSetting.channel_id = (cmbChannel.SelectedItem as PSDLib.Download.ChannelInfo).Id;
            ChannelSetting.channel_name = txtChannel.Text.Trim();
            ChannelSetting.download_state = 1;

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void cmbChannel_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbChannel.SelectedIndex >= 0)
            {
                this.txtChannel.Text = this.cmbChannel.Text;
            }
        }
    }
}
